package app.kernel.data;

import java.util.Hashtable;
import java.util.Vector;

import app.kernel.api.McsAPI;
import config.PSG_Configuration;

public class RequestsManager {

	public static Hashtable Memory;
	public static Vector QueryQueue;
	
	public String testcase1(String tName, String s1, String s2) throws Exception{
		
		System.out.println("<RequestsManager> Testcase 1 </RequestsManager>\n");
		
		return "Physical Space 1: "+tName;
		
	}

	public String testcase2(String tName){
		
		return "4fdsaf Physical Space 1: "+tName;
		
	}

	// For Query Dissemination
	public String queryP2P(String CSG_Name, String SCName, String nam, String signature, String queryString, String requestorAddress) throws Exception{
		
		String result;
		System.out.println("<RequestsManager> queryP2P </RequestsManager>\n");
		
		result = ((McsAPI)app.demo.demo.PSGs.get(nam)).queryP2P(CSG_Name, SCName, nam, signature, queryString, requestorAddress);
		
		return result;
		
		
	}
	
	// For Query Dissemination
	public String queryP2P(String CSG_Name, String SCName, String nam, String signature, String queryString, String requestorAddress, int TTL) throws Exception{
		nam = "http://"+PSG_Configuration.PSGMAC+"/xmlrpc";
		String result;
		System.out.println("<RequestsManager> queryP2P </RequestsManager>\n");
		
		result = ((McsAPI)app.demo.demo.PSGs.get(nam)).queryP2P(CSG_Name, SCName, nam, signature, queryString, requestorAddress);
		
		return result;
		
		
	}
		
	// For registering oneself as a neighbour of the requestor peer.
	public String registerNeighbour(String CSG_Name, String SCName, String nam, String requestorAddress){
		
		String result = ((McsAPI)app.demo.demo.PSGs.get(nam)).registerNeighbour(CSG_Name, SCName, requestorAddress);		
		return result;
		
	}
	
	

	//	Allow Satisfied PSG to connect to application
	public String registerResult(String nam, String result, String querySignature){
		
		((McsAPI)app.demo.demo.PSGs.get(nam)).myRequests.reportResultsToLookupRequest(querySignature, result);		
		return "";
	}

	//  Allow Satisfied CP to connect to application
	public String registerCPResult(String nam, String result, String querySignature){
		
		((McsAPI)app.demo.demo.PSGs.get(nam)).myRequests.reportResultsToLookup_CP_Request(querySignature, result);		
		return "";
		
	}
	
	
	public String requestPSGToReRegister(String CSG_Name, String SCName, String nam){
		
		System.err.println("HEHEHEHEHEHEHEHEHE ["+ nam+"]");
		System.out.println("((McsAPI)app.demo.demo.PSGs.get(nam)) = "+((McsAPI)app.demo.demo.PSGs.get(nam)));
		((McsAPI)app.demo.demo.PSGs.get(nam)).sm.re_register(CSG_Name, SCName,nam);
		
		
		
		return "DONE";
		
	}

	public String requestPSGToReRegister(String CSG_Name, String SCName, String nam, String refList){
		
		System.err.println("HEHEHEHEHEHEHEHEHE ["+ nam+"]");
		System.out.println("((McsAPI)app.demo.demo.PSGs.get(nam)) = "+((McsAPI)app.demo.demo.PSGs.get(nam)));
		((McsAPI)app.demo.demo.PSGs.get(nam)).sm.re_register(CSG_Name, SCName,nam,refList);
		
		
		

		return "DONE";
		
	}
	
	
	// Return entry point for quering the Semantic Clusters
	/*public String queryMCS(String queryString){
		
		return null;
		
	}*/
		
	
}
